package model.system;

import java.util.ArrayList;
import java.util.List;

import model.os.OSProcess;
import model.os.Page;

public class HardDisk {

	public static final int SIZE = 1000;

	private List<Slot> slots;
	
	private int nextEmptySlot;

	public HardDisk() {
		initialize();
	}

	public void initialize() {
		slots = new ArrayList<Slot>();
		Slot slot;
		for (int i = 0; i < SIZE; i++) {
			slot = new Slot();
			slots.add(slot);
		}
	}

	public void store(OSProcess process) {
		Slot availableSlot;
		for (int i = 0; i < process.getSize(); i++) {
			 availableSlot = slots.get(nextEmptySlot++);
			 availableSlot.setProcess(process);
			 availableSlot.setPage(process.getPages().get(i));
		}
	}
	
	public Page getPage(int number, OSProcess process) {
		for (Slot slot : slots) {
			if (slot.getProcess().equals(process) && slot.getPage().getNumber() == number) {
				return slot.getPage();
			}
		}
		return null;
	}

	public void setSlots(List<Slot> slots) {
		this.slots = slots;
	}

	public List<Slot> getSlots() {
		return slots;
	}
	
	public Slot getSlot(int number) {
		return slots.get(number);
	}
	
	public int size() {
		return slots.size();
	}

	public class Slot {

		private OSProcess process;

		private Page page;

		public void setProcess(OSProcess process) {
			this.process = process;
		}

		public OSProcess getProcess() {
			return process;
		}

		public void setPage(Page page) {
			this.page = page;
		}

		public Page getPage() {
			return page;
		}
		
		@Override
		public String toString() {
			return (page != null) ? String.valueOf(page.getNumber()): "";
		}
		
	}

}
